/**
 * 
 */
package com.duckygo.towerdefense.game;

import javax.microedition.khronos.opengles.GL10;

import com.duckygo.towerdefense.game.scenes.SceneManager;


import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class GameThread extends Thread {

	public boolean isRunning = false;
    
    private boolean isPaused = false;
    
    private long mLastTime = 0;

    private int fps = 15;

	private GL10 gl;
    
	
    public GameThread(Context context) {
    }

    @Override
    public void run() {
    	
		mLastTime = System.currentTimeMillis();
		int delta = -1;
		if (fps > 0) {
			delta = 1000/fps;
		}
	
		isRunning = true;
		while (isRunning) {
			if (isPaused()) {
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}
			
            long extraFps = delta - (System.currentTimeMillis() - mLastTime);
			if (extraFps > 0) {
				try {
					Thread.sleep(extraFps);
				}
				catch (InterruptedException ex) {}
				extraFps = 0;
			} else {
				Log.w("GameThread", "ExtraFPS : " + extraFps);
			}
			SceneManager.getInstance().update(delta + extraFps);
			
            mLastTime = System.currentTimeMillis();
            
		}
    }
    

	public void setPaused(boolean isPaused) {
		this.isPaused = isPaused;
	}

	public boolean isPaused() {
		return isPaused;
	}

	public void setSurface(GL10 gl, int width, int height) {
		this.gl = gl;
	}

}